package com.jay.lang.intf;

import java.util.Calendar;
import java.util.Formatter;
import java.util.GregorianCalendar;
import java.util.Locale;

public class AppendableIntf {
	public static void main(String[] args) {

		// StringBuilder实现了Appendable接口
		StringBuilder sb = new StringBuilder();
		// Send all output to the Appendable object sb
		Formatter formatter = new Formatter(sb, Locale.US);

		// Explicit argument indices may be used to re-order output.
		// 将格式化后的字符串放入sb

		// Optional locale as the first argument can be used to get
		// locale-specific formatting of numbers. The precision and width can be
		// given to round and align the value.
		formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
		// -> "e =    +2,7183"
		System.out.println(sb.toString());

		// The '(' numeric flag may be used to format negative numbers with
		// parentheses rather than a minus sign. Group separators are
		// automatically inserted.
		float balanceDelta = 6217.578f;
		formatter.format(
				"Amount gained or lost since last statement: $ %(,.2f",
				balanceDelta);
		// -> "Amount gained or lost since last statement: $ (6,217.58)"

		System.out.println(sb.toString());
		System.out.println(sb.append('c').toString());

		// Writes a formatted string to System.out.
		System.out.format("Local time: %tT", Calendar.getInstance());
		// -> "Local time: 13:34:18"

		// Writes formatted output to System.err.
		// System.err.printf("Unable to open file '%1$s': %2$s", fileName,
		// exception.getMessage());
		// -> "Unable to open file 'food': No such file or directory"

		Calendar c = new GregorianCalendar(1995, 5, 23);
		String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
		// -> s == "Duke's Birthday: May 23, 1995"

		System.out.println(s);

	}
}
